/*   
 * Copyright 2010 Rob Schoening
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 *    
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.beaconframework.vaadin.ui;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

public class AppBuilder {

    private Logger log = LoggerFactory.getLogger(AppBuilder.class);
    private List<FeatureInjector> injectors = new CopyOnWriteArrayList<FeatureInjector>();

    public void addFeatureInjector(FeatureInjector fi) {
        injectors.add(fi);
    }

    public void addInjectorsFromSpring(ApplicationContext ctx) {
        for (Map.Entry<String, FeatureInjector> entry : ctx.getBeansOfType(
                FeatureInjector.class).entrySet()) {
            log.info("Found feature injector: " + entry.getKey());
            if (injectors.contains(entry.getValue())) {
                log.info("Feature injector already present: "
                        + entry.getValue());
            } else {
                injectors.add(entry.getValue());
            }
        }
    }

    public void injectFeatures() {
        Collections.sort(injectors);

        for (FeatureInjector fd : injectors) {

            if (fd.isEnabled()) {
                fd.inject();
            } else {
                // log
            }

        }

    }
}
